package hu.rsc.feeds.client.gui.generated;

import hu.rsc.feeds.client.Feeds;
import hu.rsc.feeds.client.data.Dictionary;
import hu.rsc.feeds.client.data.GeneratedEntry;
import hu.rsc.feeds.client.data.SystemFeed;
import hu.rsc.feeds.client.gui.FeedPanel;
import hu.rsc.feeds.client.gui.richTextArea.RichTextAreaWithToolbar;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class GeneratedEntryForm extends Composite {
    private FeedPanel parent;

    private FlexTable form = new FlexTable();

    //private TextArea descTa;
    private RichTextAreaWithToolbar descTa;
    private TextArea sumTa;
    private TextBox titleTb;
    private TextBox linkTb;

    private GeneratedEntry entry = new GeneratedEntry();

    public GeneratedEntryForm(FeedPanel parent) {
	this.parent = parent;
	buildContent();
	initWidget(this.form);
    }

    public GeneratedEntry getFeed() {
	return this.entry;
    }

    public void refresh(GeneratedEntry entry) {
	this.entry = entry;
	buildContent();
    }

    private void buildContent() {
	int row = 0;
	this.form.clear();
	this.form.addStyleName("form");
	this.form.addStyleName("border");
	Label lb = new Label();
	//lb.addStyleName("formlabel");
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_GEN_TITLE, lb));
	this.form.setWidget(row, 0, lb);
	row++;

	this.titleTb = new TextBox();
	this.titleTb.addStyleName("forminputmaxwidth");
	this.form.setWidget(row, 0, this.titleTb);
	this.titleTb.setValue(this.entry.getTitle());
	row++;

	lb = new Label();
	//lb.addStyleName("formlabel");
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_GEN_DESC, lb));
	this.form.setWidget(row, 0, lb);
	row++;

	//this.descTa = new TextArea();
	this.descTa = new RichTextAreaWithToolbar();
	this.descTa.addStyleName("desctextarea");
	//this.descTa.setVisibleLines(10);
	this.form.setWidget(row, 0, this.descTa);
	this.descTa.setValue(this.entry.getDescription());
	row++;

	if (!isSystem()) {
	    lb = new Label();
	    //lb.addStyleName("formlabel");
	    lb.setText(Dictionary.getEntry(Dictionary.LABEL_GEN_LINK, lb));
	    this.form.setWidget(row, 0, lb);
	    row++;
	    this.linkTb = new TextBox();
	    this.linkTb.addStyleName("forminputmaxwidth");
	    this.linkTb.setWidth("100%");
	    this.form.setWidget(row, 0, this.linkTb);
	    this.linkTb.setValue(this.entry.getLink());
	} else {
	    lb = new Label();
	    //lb.addStyleName("formlabel");
	    lb.setText(Dictionary.getEntry(Dictionary.LABEL_GEN_CONTEXT_CHANNEL_URL, lb));
	    this.form.setWidget(row, 0, lb);
	    row++;
	    this.linkTb = new TextBox();
	    this.linkTb.addStyleName("forminputmaxwidth");
	    this.linkTb.setWidth("100%");

	    this.form.setWidget(row, 0, this.linkTb);
	    Long fid = this.entry.getId();
	    SystemFeed feed = ((SystemPanel) this.parent).getFeed();
	    if (fid == null || fid < 0) {
		this.linkTb.setText("N/A");
	    } else {
		if (feed.getHelp()) {
		    this.linkTb.setValue(SystemForm.HELP_URL + feed.getApplication() + "&entry=" + this.entry.getId());
		} else {
		    this.linkTb.setValue(SystemForm.NEWS_URL + feed.getApplication() + "&entry=" + this.entry.getId());
		}
	    }
	    this.linkTb.setReadOnly(true);
	}
	this.form.getFlexCellFormatter().setColSpan(row, 0, 2);
	row++;

	HorizontalPanel buttonPanel = new HorizontalPanel();
	buttonPanel.addStyleName("buttonpanel");
	this.form.setWidget(row, 0, buttonPanel);
	this.form.getFlexCellFormatter().setColSpan(row, 0, 2);
	row++;

	Button save = new Button();
	save.setText(Dictionary.getEntry(Dictionary.BUTTON_EDIT_PROFILE, save));
	buttonPanel.add(save);

	save.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		if (validate()) {
		    GeneratedEntryForm.this.parent.saveEntry(GeneratedEntryForm.this.entry);
		}
	    }
	});

	Button newBt = new Button();
	newBt.setText(Dictionary.getEntry(Dictionary.BUTTON_NEW_ARTICLE, save));
	buttonPanel.add(newBt);
	newBt.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		refresh(new GeneratedEntry());
	    }
	});

	Button delBt = new Button();
	delBt.setText(Dictionary.getEntry(Dictionary.BUTTON_DELETE, save));
	delBt.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		Feeds.getService().deleteGeneratedEntry(GeneratedEntryForm.this.entry.getId(), new AsyncCallback<String>() {

		    @Override
		    public void onFailure(Throwable caught) {
			Window.alert(Dictionary.getEntry(Dictionary.MESSAGE_COMM_ERROR, null));
		    }

		    @Override
		    public void onSuccess(String result) {
			if (result != null) {
			    Window.alert(Dictionary.getEntry(result, null));
			} else {
			    refresh(new GeneratedEntry());
			    GeneratedEntryForm.this.parent.refreshEntityTable();
			}
		    }
		});
	    }
	});
	buttonPanel.add(delBt);

    }

    private boolean isSystem() {
	return !(this.parent instanceof GeneratedPanel);
    }

    private boolean validate() {
	if (isSystem()) {
	    SystemFeed feed = ((SystemPanel) this.parent).getFeed();
	    if (feed.getHelp()) {
		this.linkTb.setValue(SystemForm.HELP_URL + feed.getApplication() + "&entry=" + this.entry.getId());
	    } else {
		this.linkTb.setValue(SystemForm.NEWS_URL + feed.getApplication() + "&entry=" + this.entry.getId());
	    }
	}

	this.entry.setDescription(this.descTa.getValue());
	this.entry.setLink(this.linkTb.getValue());
	this.entry.setTitle(this.titleTb.getValue());
	this.entry.setFeed(this.parent.getFeedId());

	StringBuilder error = new StringBuilder();
	if (this.entry.getFeed() == null) {
	    error.append(Dictionary.getEntry(Dictionary.MESSAGE_NO_SELECTED_CHANNEL, null)).append("\r\n");
	}
	if (this.titleTb.getValue() == null || this.titleTb.getValue().length() < 1) {
	    error.append(Dictionary.getEntry(Dictionary.MESSAGE_TILE_REQUIRED, null)).append("\r\n");
	}

	if (error.length() > 0) {
	    Window.alert(error.toString());
	    return false;
	}
	return true;
    }
}
